<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/21
 * Time: 11:14
 */

namespace app\admin\controller;
use app\admin\model\Base;
use think\Controller;
use think\Db;
use think\Request;

class Activity extends Common
{
    public $obj = "";
    public $table = "goods_activity";
    public $date = "";
public $go="activity";
    public function __initialize()
    {
        parent::__initialize();

    }

    public function show()
    {
        //每页显示10条数据
        $list = Db::table('goods_activity')->where("deleted=2")->paginate(10);
       //分页
        $page=$list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        //分页输出显示
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch($this->go.'/show');
    }


//添加数据页面
    public function add()
    {
        $list = Db::table('ategory')->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    //添加数据信息
    public function addm()
    {
        $object = new Base();
        $table = $this->table;
        $title = I('title');
        $cintent = I('content');
        $goods_image = I('goods_image');
        $goods_class_name = I('goods_class_name');
        $deleted = I('deleted');
        $sort = I('sort');
        $data = ['title' => $title, 'content' => $cintent, 'goods_image' => $goods_image, 'goods_class_name' => $goods_class_name, 'time' => time(), 'deleted' => $deleted, 'sort' => $sort];
        $obj = $object->add($table, $data);
        if ($obj) {
            $this->success('添加成功', url('Activity/show'));
        } else {
            $this->error('添加失败');
        }
    }




//信息删除
    public function dels()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : "";
        $data = ['deleted' => 1];
        $del = new Base();
        $del = $del->status('goods_activity', $id, $data);
        return json_encode($id);
    }


//修改内容页面
    public function update()
    {
        $id = input('id');
        //var_dump($id);
        $info = Db::table('goods_activity')->where("id=$id")->select();
        $this->assign('info', $info);
       // var_dump($info);

        return $this->fetch('update');
    }

    //修改内容
    public function updatem()
    {
        $object = new Base();
        $table = $this->table;
        $id = input('id');
        $date = Request::instance()->post();
        $date = array(
            "id" => $date['id'],
            "goods_name" => $date['goods_name'],
            "goods_price" => $date['goods_price'],
            "goods_details" => $date['goods_details'],
            "goods_number" => $date['goods_number'],
            "goods_class_id" => $date['goods_class_id'],
            "goods_appraise" => $date['goods_appraise'],
            "goods_image_id" => $date['goods_image_id'],
            "promotion_money" => $date['promotion_money'],
            "promotion_money_vip" => $date['promotion_money_vip'],
            "deleted" => $date['deleted'],
            "sort" => $date['sort'],
        );
        $obj = $object->upd($table, $id, $date);
        if ($obj) {
            $this->success('修改成功', url('Activity/show'));
        } else {
            $this->error('修改失败');
        }
    }


//多图片上传
    public function uploads()
    {
        header('content-type:text/html charset:utf-8');
        $dir_base = ROOT_PATH."public/admin/images/"; 	//文件上传根目录
        //没有成功上传文件，报错并退出。
        if(empty($_FILES)) {
            echo "<textarea><img src='{$dir_base}error.jpg'/></textarea>";
            exit(0);
        }
        $base  = request()->root();
        $root  = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
        if ('' != $root) {
            $root = '/' . ltrim($root, '/');
        }
        $output = "";
        $index = 0;		//$_FILES 以文件name为数组下标，不适用foreach($_FILES as $index=>$file)
        foreach($_FILES as $file){
            $upload_file_name = 'upload_file' . $index;		//对应index.html FomData中的文件命名
            $filename = $_FILES[$upload_file_name]['name'];
            $gb_filename = iconv('utf-8','gb2312',$filename);	//名字转换成gb2312处理
            //文件不存在才上传
            if(!file_exists($dir_base.$gb_filename)) {
                $isMoved = false;  //默认上传失败
                $MAXIMUM_FILESIZE = 10 * 1024 * 1024; 	//文件大小限制	1M = 1 * 1024 * 1024 B;
                $rEFileTypes = "/^\.(jpg|jpeg|gif|png){1}$/i";
                if ($_FILES[$upload_file_name]['size'] <= $MAXIMUM_FILESIZE &&
                    preg_match($rEFileTypes, strrchr($gb_filename, '.'))) {
                    $isMoved = @move_uploaded_file ( $_FILES[$upload_file_name]['tmp_name'], $dir_base.$gb_filename);		//上传文件

                }
            }else{
                $isMoved = true;	//已存在文件设置为上传成功
            }
            if($isMoved){
                //输出图片文件<img>标签
                //注：在一些系统src可能需要urlencode处理，发现图片无法显示，
                //    请尝试 urlencode($gb_filename) 或 urlencode($filename)，不行请查看HTML中显示的src并酌情解决。
//                 $url="{$dir_base}{$filename}";
                $url = $root.DS.'admin'.DS.'images'.DS.$filename;
                $urlOne= $root.DS.'admin'.DS.'images'.DS."error.jpg";
                 $newUrl=str_replace('\\','/',$url);
                $output .= "<img src='$newUrl' style='width:500px;height:100px;' />";
            }else {
                $output .= "<img src=$urlOne title='{$filename}' alt='{$filename}'/>";
            }

            $index++;
        }
        $insertId= Db::table("goods_activity")->field("id")->order("id desc")->select();
        $endID=$insertId['0']['id']+1;
        $date=['goods_activity_address'=>$url,'goods_activity_id'=>$endID];
        Db::table("goods_activity_image")->insert($date);
        echo  json_encode($output."");


    }
}